/*
 * SupportsMessage.java
 *
 * Created on June 17, 2007, 9:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jhub.text;

import java.text.MessageFormat;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import jhub.DCProtocol;
import jhub.Support;

/**
 *
 * @author dbotelho
 */
public class SupportsMessage extends DCRawCommand{
    
    public static SupportsMessage getInstance(Support ... supports) throws ParseException{
        StringBuffer sb = new StringBuffer();
        for(Support support : supports){
            sb.append(support.toString()+" ");
        }
        return new SupportsMessage(MessageFormat.format(DCProtocol.SUPPORTS_FORMAT,sb.toString()));
    }
    
    private List<Support> supports;
    
    public SupportsMessage(String command) throws ParseException {
        super(command);
        this.supports =new LinkedList<Support>();
        parse();
    }
    
    private void parse() throws ParseException{
        Object[] o = new MessageFormat(DCProtocol.SUPPORTS_FORMAT).parse(command.toString());
        StringTokenizer tokens = new StringTokenizer(o[0].toString()," ");
        while(tokens.hasMoreTokens()){
            try {
                getSupports().add(Support.valueOf(tokens.nextToken()));
            } catch(java.lang.IllegalArgumentException ex) {
                ex.printStackTrace();
            }
        }
    }

    public List<Support> getSupports() {
        return supports;
    }
}
